package com.newspublish.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.newspublish.system.entity.system.Collect;
import com.newspublish.system.entity.system.Comment;
import com.newspublish.system.entity.system.News;
import com.newspublish.system.mapper.CollectMapper;
import com.newspublish.system.service.CollectService;
import com.newspublish.system.utils.result.Result;
import com.newspublish.system.utils.util.GetMassege;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect> implements CollectService {

    @Autowired
    private CollectMapper collectMapper;

    @Autowired
    private GetMassege getMassege;

    @Override
    public Result collectNews(Long newsId) {
        String user= getMassege.getUser();
        Collect collect=new Collect();
        collect.setUsername(user);
        collect.setNewsId(newsId);
        collectMapper.insert(collect);
        return Result.ok(collect).message("用户收藏新闻成功");
    }

    @Override
    public Result cancelNews(Long newsId) {
        int delete=collectMapper.deleteById(newsId);
        if(delete>0){
            return Result.ok().message("删除新闻成功");
        }else{
            return Result.fail().message("删除新闻失败");
        }
    }

    @Override
    public Result getAllCollection() {
        String user=getMassege.getUser();
        List<News> newsList=collectMapper.selectByUsername(user);
        System.out.println(newsList);
        return Result.ok(newsList).message("返回数据成功");
    }
}
